package com.xplay.xpocker.room.mahjong.dto;

import com.xplay.xpocker.constant.ConstantDict;
import com.xplay.xpocker.dvo.GameRuMahjongUserState;
import com.xplay.xpocker.dvo.RoomInfoInterface;
import com.xplay.xpocker.entity.GameRuMahjong;
import com.xplay.xpocker.util.ObjectUtils;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class MahjongRoomInfo extends GameRuMahjong implements RoomInfoInterface {
    // 默认其它用户  通过CurrentUser.getUserId  确定
    private List<GameRuMahjongUserState> otherUsers;
    // 默认当前用户 通过  load 添加
    private GameRuMahjongUserState currentUser;

    private List<GameRuMahjongUserState> allUser;
    int residue;

    GameRuMahjong ruInfo;


    /**
     * 下面三个重写是给前端的数据
     * 请勿删除
     *
     * @return
     */
    public List<Integer> diskPartAllCard() {
        if (ObjectUtils.isEmpty(getCards())) {
            return null;
        }
        return Arrays.stream(getCards().split(ConstantDict.Separator.comma)).map(Integer::valueOf).collect(Collectors.toList());
    }

    public void printAllCard(String cardCode) {
        StringBuilder cardInfoBuilder = new StringBuilder("\n");
        cardInfoBuilder.append("房间号     :").append(getCode()).append("\n");
        cardInfoBuilder.append("发出卡牌   :").append(cardCode).append("\n");
        cardInfoBuilder.append("剩余卡牌信息:").append(getCards()).append("\n");
        for (GameRuMahjongUserState userState : getAllUser()) {
            cardInfoBuilder.append("用户:").append(userState.getUsername()).append(",手牌:").append(userState.allHandCardsStr());
            if (ObjectUtils.notEmpty(userState.getOpenDeal())) {
                cardInfoBuilder.append("碰杠信息").append(userState.getOpenDeal().stream().map(item -> String.format("__卡牌(%s,%s)", item.getCard(), item.getType())).collect(Collectors.toList()));
            }
            cardInfoBuilder.append("\n");
        }
        log.debug(cardInfoBuilder.toString());
    }


    /**
     * 下面三个重写是给前端的数据
     * 请勿删除
     *
     * @return
     */
    public List<Integer> getDiskPartExportAllCard() {
        List<Integer> result = new ArrayList<>();
        if (ObjectUtils.isEmpty(getAllUser())) {
            return null;
        }
        for (GameRuMahjongUserState userDTO : getAllUser()) {
            if (ObjectUtils.isEmpty(userDTO.getExportCards())) {
                continue;
            }
            result.addAll(Arrays.stream(userDTO.getExportCards().split(ConstantDict.Separator.comma)).map(Integer::valueOf).collect(Collectors.toList()));
        }
        return result;
    }

    public int getResidue() {
        return this.getCards() == null ? 0 : this.getCards().split(ConstantDict.Separator.comma).length;
    }

    public void setResidue(int residue) {
        this.residue = residue;
    }

    public GameRuMahjongUserState getUserById(BigDecimal uid) {
        if (uid == null || allUser == null) {
            return null;
        }
        return allUser.stream().filter(item -> item.getUserId().equals(uid)).findAny().orElse(null);
    }

    public List<GameRuMahjongUserState> getOtherUserById(BigDecimal uid) {
        if (uid == null || allUser == null) {
            return null;
        }
        return allUser.stream().filter(item -> !item.getUserId().equals(uid)).collect(Collectors.toList());
    }

    /**
     * 通过  uid  重新初始化
     * 当前用户  和  其他 用户
     *
     * @param uid
     */
    public void reInitUser(BigDecimal uid) {
        currentUser = getUserById(uid);
        otherUsers = getOtherUserById(uid);
    }

    @Override
    public Integer getVersion() {
        return super.getVersion();
    }


    public List<GameRuMahjongUserState> getOtherUsers() {
        return otherUsers;
    }

    public void setOtherUsers(List<GameRuMahjongUserState> otherUsers) {
        this.otherUsers = otherUsers;
    }

    public GameRuMahjongUserState getCurrentUser() {
        return currentUser;
    }

    public void setCurrentUser(GameRuMahjongUserState currentUser) {
        this.currentUser = currentUser;
    }

    public List<GameRuMahjongUserState> getAllUser() {
        return allUser;
    }

    public void setAllUser(List<GameRuMahjongUserState> allUser) {
        this.allUser = allUser;
    }

    public GameRuMahjong getRuInfo() {
        return ruInfo;
    }

    public void setRuInfo(GameRuMahjong ruInfo) {
        this.ruInfo = ruInfo;
    }
}
